/**
 * Copyright (C) 2009 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.template.flow;

import static cc.aileron.template.flow.FlowCategory.*;

import java.util.EnumMap;

import cc.aileron.template.context.TemplateContext;

import com.google.inject.Inject;
import com.google.inject.Singleton;

/**
 * @author Aileron
 * 
 */
@Singleton
public class FlowMethodProviderMapImpl implements FlowMethodProviderMap
{

    /*
     * (非 Javadoc)
     * 
     * @see
     * cc.aileron.template.flow.FlowMethodProviderMap#get(cc.aileron.template
     * .flow.FlowCategory)
     */
    public FlowMethodProvider get(final FlowCategory category)
    {
        return map.get(category);
    }

    /**
     * constractor
     * 
     * @param def
     * @param each
     */
    @Inject
    public FlowMethodProviderMapImpl(final FlowDef def, final FlowEach each)
    {
        map = new EnumMap<FlowCategory, FlowMethodProvider>(FlowCategory.class);
        map.put(DEF, def);
        map.put(EACH, each);
    }

    private final EnumMap<FlowCategory, FlowMethodProvider> map;
}

class FlowDef implements FlowMethodProvider
{
    public FlowDefMethod get(final String args)
    {
        return new FlowDefMethod()
        {
            public boolean call(final TemplateContext context)
            {
                return context.getAccessor().get(args, Boolean.class);
            }
        };
    }
}

class FlowEach implements FlowMethodProvider
{
    public FlowEachMethod get(final String args)
    {
        return new FlowEachMethod()
        {
            public Iterable<?> call(final TemplateContext context)
            {
                return context.getAccessor().get(args, Iterable.class);
            }
        };
    }
}